using System;
using System.Drawing;
using System.Collections.Generic;
using System.Text;
using Microsoft.DirectX.Direct3D;
using TTDotNet.Util;
using TTDotNet.Engine.Terrain;
using TTDotNet.Engine.GUI;
using TTDotNet.Engine.GUI.Buttons;
using TTDotNet.Engine.Create.Tools;

namespace TTDotNet.Engine.GUI.Windows.Road
{
    public class BuildBusStationWindow : Window
    {
        private int bSize = 96;
        private int cSize = 24;
        private int border = 2;
        
        public BuildBusStationWindow(PointF screenLocation)
        {
            int width = (2 * bSize) + (3 * border);
            int height = cSize + (3 * bSize) + (3 * border);

            SetScreenRectangle(new RectangleF(screenLocation, new SizeF(width, height)));

            int controlNum = 0;

            windowControls[controlNum++] = new Label(new RectangleF(new PointF(0, 0), new SizeF(width, height)), "", 1, Color.Black, "Arial",
                                            Game.textureCacher.LoadTexture("artwork/colours/dark_spring_green.jpg"), DrawTextFormat.Center | DrawTextFormat.VerticalCenter);

            //Close button. 
            windowControls[controlNum++] = new CloseButton(new RectangleF(new PointF(border, border), new SizeF(cSize, cSize)),
                                            Game.textureCacher.LoadTexture("artwork/128/close.jpg"), Game.textureCacher.LoadTexture("artwork/128/closeDown.jpg"), this);

            windowControls[controlNum++] = new TitleBar(new RectangleF(new PointF(cSize + (2 * border), border), new SizeF(width - (cSize + (3 * border)), cSize)),
                                            "Bus Station Orientation", 13, Color.White, "Arial", Game.textureCacher.LoadTexture("artwork/colours/ttdOrange.jpg"), this);

            RadioGroup group = new RadioGroup();
            windowControls[controlNum++] = group;

            int rbNum = 0;

            RadioButton north = new RadioButton(1, new RectangleF(new PointF(border, cSize + (2 * border)), new SizeF(bSize, bSize)),
                                            "", 1, Color.Black, "Arial", false, true, Game.textureCacher.LoadTexture("artwork/128/bus_station_n.png"),
                                            Game.textureCacher.LoadTexture("artwork/128/bus_station_n_down.png"));
            north.OnControl += new Control.ControlDelegate(BuildBusStation);
            north.Tag = RotateType.Zero;
            group.Add(rbNum++, north);

            RadioButton east = new RadioButton(1, new RectangleF(new PointF(2 * border + bSize, cSize + (2 * border)), new SizeF(bSize, bSize)),
                                            "", 1, Color.Black, "Arial", false, true, Game.textureCacher.LoadTexture("artwork/128/bus_station_e.png"),
                                            Game.textureCacher.LoadTexture("artwork/128/bus_station_e_down.png"));
            east.OnControl += new Control.ControlDelegate(BuildBusStation);
            east.Tag = RotateType.Ninety;
            group.Add(rbNum++, east);

            RadioButton south = new RadioButton(1, new RectangleF(new PointF(2 * border + bSize, cSize + (3 * border) + bSize), new SizeF(bSize, bSize)),
                                            "", 1, Color.Black, "Arial", false, true, Game.textureCacher.LoadTexture("artwork/128/bus_station_s.png"),
                                            Game.textureCacher.LoadTexture("artwork/128/bus_station_s_down.png"));
            south.OnControl += new Control.ControlDelegate(BuildBusStation);
            south.Tag = RotateType.OneEighty;
            group.Add(rbNum++, south);

            RadioButton west = new RadioButton(1, new RectangleF(new PointF(border, cSize + (3 * border) + bSize), new SizeF(bSize, bSize)),
                                            "", 1, Color.Black, "Arial", false, true, Game.textureCacher.LoadTexture("artwork/128/bus_station_w.png"),
                                            Game.textureCacher.LoadTexture("artwork/128/bus_station_w_down.png"));
            west.OnControl += new Control.ControlDelegate(BuildBusStation);
            west.Tag = RotateType.TwoSeventy;
            group.Add(rbNum++, west);
        }

        private void BuildBusStation(object data, object tag)
        {
            ControlStateChange state = (ControlStateChange)data;
            
            if (state == ControlStateChange.Selected)
            {
                RotateType rotType = (RotateType)tag;

                (Game.toolbox.GetTool(ToolType.BusStation) as BusStationTool).SetRotation(rotType);
                Game.toolbox.ChangeToolStatus(ToolType.BusStation, true);
            }
            else if (state == ControlStateChange.Deselected)
            {
                Game.toolbox.ChangeToolStatus(ToolType.BusStation, false);
            }
        }

        public override void Close()
        {
            Game.toolbox.ChangeToolStatus(ToolType.BusStation, false);

            base.Close();
        }

    }
}
